package edu.gduf.service.impl;

import edu.gduf.dao.ActivityDao;
import edu.gduf.domain.Activity;
import edu.gduf.domain.Catalog;
import edu.gduf.domain.ResultInfo;
import edu.gduf.service.ActivityService;
import edu.gduf.utils.DateUtil;
import edu.gduf.utils.SensitiveWordsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 谢佳豪
 * @data 2020/10/9 - 17:31
 * @target 活动模块业务层接口实现类
 */
@Service
public class ActivityServiceImpl implements ActivityService {


    /**
     * 声明分页一页的数据量为10条
     */
    private Integer pageSize = 10;

    @Autowired
    private ActivityDao activityDao;


    /**
     * 活动发布（添加活动信息）
     *
     * @param activity    前端表单提交activity数据
     * @param httpSession httpSession域
     * @return json数据
     */
    @Override
    public ResultInfo addActivity(Activity activity, HttpSession httpSession) {
        //TODO 需要登录完成存储aid到httpSession域
        //从httpSession域中取出uid值
        Integer aid = (Integer) httpSession.getAttribute("uid");

        //设置表单不能够直接填写的值
        activity.setAid(aid);
        activity.setPublish_time(DateUtil.getCurrentTime());
        activity.setStatus(1);
        activity.setView(1);

        //获取需要用户填写的信息，进行敏感词检测
        boolean rightTitle = SensitiveWordsUtil.isContaintBadWord(activity.getTitle(), 2);
        boolean rightContent = SensitiveWordsUtil.isContaintBadWord(activity.getContent(), 2);

        if (rightTitle) {
            return ResultInfo.failInfo("活动发布失败！活动标题存在敏感词汇！");
        } else if (rightContent) {
            return ResultInfo.failInfo("活动发布失败！活动内容存在敏感词汇！");
        } else {
            //调用dao层接口进行数据添加
            int i = activityDao.addActivity(activity);
            //0-失败；1-成功
            if (i == 1) {
                return ResultInfo.successInfo("活动发布成功！");
            } else {
                return ResultInfo.failInfo("活动发布失败！数据存在错误，请进行检查！");
            }
        }
    }

    /**
     * 查询活动的相关目录
     * @return
     */
    @Override
    public ResultInfo catalogList() {
        List<Catalog> catalogList = activityDao.catalogList();
        return ResultInfo.successInfo("查询成功！", catalogList);
    }

    /**
     * 活动查询（全部-分页）
     *
     * @param currentPageIndex 当前页面下标值
     * @return json数据
     */
    @Override
    public ResultInfo selectActivityList(Integer currentPageIndex) {
        //查询下标值
        Integer startIndex = (currentPageIndex - 1) * pageSize;

        //调用dao层接口进行数据查询
        List<Activity> activityList = activityDao.selectActivityList(startIndex, pageSize);

        //查询所有活动的数据量
        Integer totalNumber = activityDao.totalNumber();
        //页数
        Integer pageCount = (totalNumber % pageSize) != 0 ? (totalNumber / pageSize) + 1 : (totalNumber / pageSize);

        //将数据以键值对的形式存入到map中
        Map<String, Object> map = new HashMap<>();

        map.put("pageCount", pageCount);
        map.put("activityList", activityList);


        if (activityList.size() == 0) {
            return ResultInfo.successInfo("数据查询成功！数据量为0！");
        } else {
            return ResultInfo.successInfo("数据查询成功！", map);
        }
    }

    /**
     * 活动查询（条件）
     *
     * @param cid              分类目录的id
     * @param currentPageIndex 当前页面下标值
     * @return json
     */
    @Override
    public ResultInfo selectActivitiesByCid(Integer cid, Integer currentPageIndex) {
        //查询下标值
        Integer startIndex = (currentPageIndex - 1) * pageSize;

        //调用dao层接口进行数据查询
        List<Activity> activityList = activityDao.selectActivitiesByCid(cid, startIndex, pageSize);

        //查询cid-活动的数据量
        Integer totalNumberByCid = activityDao.totalNumberByCid(cid);
        //页数
        Integer pageCount = (totalNumberByCid % pageSize) != 0 ? (totalNumberByCid / pageSize) + 1 : (totalNumberByCid / pageSize);

        //将数据以键值对的形式存入到map中
        Map<String, Object> map = new HashMap<>();

        map.put("pageCount", pageCount);
        map.put("activityList", activityList);

        if (activityList.size() == 0) {
            return ResultInfo.successInfo("数据查询成功！数据量为0！");
        } else {
            return ResultInfo.successInfo("数据查询成功！", map);
        }
    }

    /**
     * 根据组织id查询其所拥有的活动信息
     *
     * @param aid              association表id主键
     * @param currentPageIndex 当前页面下标
     * @return json
     */
    @Override
    public ResultInfo selectActivitiesByAid(Integer aid, Integer currentPageIndex) {
        //查询下标值
        Integer startIndex = (currentPageIndex - 1) * pageSize;

        //调用dao层接口进行数据查询
        List<Activity> activities = activityDao.selectActivitiesByAid(aid, startIndex, pageSize);

        //查询aid-活动的数据量
        Integer totalNumberByAid = activityDao.totalNumberByAid(aid);
        //页数
        Integer pageCount = (totalNumberByAid % pageSize) != 0 ? (totalNumberByAid / pageSize) + 1 : (totalNumberByAid / pageSize);

        //将数据以键值对的形式存入到map中
        Map<String, Object> map = new HashMap<>();

        map.put("pageCount", pageCount);
        map.put("list", activities);

        if (activities.size() == 0) {
            return ResultInfo.successInfo("数据查询成功！数据量为0！");
        } else {
            return ResultInfo.successInfo("数据查询成功！", map);
        }
    }

    /**
     * 根据activity_id查询单条活动详细信息--同时更新view浏览量
     *
     * @param id activity表主键id
     * @return json
     */
    @Override
    public ResultInfo selectActivityById(Integer id) {
        //查询activity_id => activity
        Activity activity = activityDao.selectActivityById(id);
        //根据activity_id => addViews

        activityDao.addViews(activity.getView() + 1, id);

        return ResultInfo.successInfo("数据查询成功！", activity);
    }

    /**
     * 根据title模糊查询活动信息数据
     *
     * @param title            标题信息
     * @param currentPageIndex 当前页面下标值
     * @return json
     */
    @Override
    public ResultInfo selectActivitiesByTitle(String title, Integer currentPageIndex) {
        //查询下标值
        Integer startIndex = (currentPageIndex - 1) * pageSize;

        //调用dao层接口进行数据查询
        List<Activity> activityList = activityDao.selectActivitiesByTitle(title, startIndex, pageSize);

        //模糊查询title-活动的数据量
        Integer totalNumberByTitle = activityDao.totalNumberByTitle(title);
        //页数
        Integer pageCount = (totalNumberByTitle % pageSize) != 0 ? (totalNumberByTitle / pageSize) + 1 : (totalNumberByTitle / pageSize);

        //将数据以键值对的形式存入到map中
        Map<String, Object> map = new HashMap<>();

        map.put("pageCount", pageCount);
        map.put("activityList", activityList);

        if (activityList.size() == 0) {
            return ResultInfo.successInfo("数据查询成功！数据量为0！");
        } else {
            return ResultInfo.successInfo("数据查询成功！", map);
        }
    }

    /**
     * 活动更新
     *
     * @param activity 活动对象信息
     * @return json
     */
    @Override
    public ResultInfo updateActivity(Activity activity) {
        //对更新的活动信息，进行敏感词检测
        boolean rightTitle = SensitiveWordsUtil.isContaintBadWord(activity.getTitle(), 2);
        boolean rightContent = SensitiveWordsUtil.isContaintBadWord(activity.getContent(), 2);

        if (rightTitle) {
            return ResultInfo.failInfo("活动更新失败！活动标题存在敏感词汇！");
        } else if (rightContent) {
            return ResultInfo.failInfo("活动更新失败！活动内容存在敏感词汇！");
        } else {
            //调用dao层接口进行数据更新
            int i = activityDao.updateActivity(activity);
            //0-失败；1-成功
            if (i == 1) {
                return ResultInfo.successInfo("活动更新成功！");
            } else {
                return ResultInfo.failInfo("活动更新失败！数据存在错误，请进行检查！");
            }
        }
    }

    /**
     * 根据activity_id 删除活动信息
     *
     * @param id activity_id
     * @return json
     */
    @Override
    public ResultInfo delete(Integer id) {
        //调用dao接口
        Integer i = activityDao.delete(id);
        if (i == 1) {
            return ResultInfo.successInfo("信息删除成功！");
        } else {
            return ResultInfo.failInfo("信息删除失败！");
        }
    }

    /**
     * 选择合适的用户，减少招募人数需求，将该用户添加到队伍中
     *
     * @param id activity_id
     * @return json
     */
    @Override
    public ResultInfo reducePopulation(Integer id, String username) {
        //根据activity_id ==> activity
        Activity activity = activityDao.selectActivityById(id);

        int population = activity.getPopulation() - 1;

        //获取到list
        String list = activityDao.selectList(id);
        //更新list
        String newList = list + "," + username;


        //判断人数-1之后是否小于0，若小于0就直接返回信息，不进行人数数据修改
        if (population < 0) {
            //当人数已经满足需求时，将活动的状态修改为0过期（达到要求）
            activityDao.updateStatus(0, id);
            return ResultInfo.failInfo("人数已满！请勿超过您所设置的活动人数！");
        }

        //选择用户，population - 1
        activityDao.reducePopulation(population, id);
        activityDao.updateList(id, newList);
        return ResultInfo.successInfo("他/她已经成为您的成员！");
    }

    /**
     * 根据发布时间和结束时间的时间段，判断是否时间到了，进行修改活动状态
     *
     * @param timeout 时间标识符
     * @param id      activity_id
     * @return json
     */
    @Override
    public ResultInfo updateStatus(Boolean timeout, Integer id) {
        if (timeout) {
            Integer i = activityDao.updateStatus(0, id);
            if (i == 1) {
                return ResultInfo.successInfo("活动时间截止了！");
            } else {
                return ResultInfo.failInfo("内部数据错误！请联系管理员！");
            }
        } else {
            return ResultInfo.failInfo("活动还在招募中！");
        }
    }

    /**
     * 根据uid查询主题数
     * @param uid
     * @return
     */
    @Override
    public ResultInfo searchNum(Integer uid) {
        Integer num = activityDao.searchNum(uid);
        return ResultInfo.successInfo("查询成功！", num);
    }
}
